package com.example.orderflow.controller.ordergenerate;

import com.example.orderflow.controller.base.ABaseController;
import com.example.orderflow.entity.constants.OrderInfoConstants;
import com.example.orderflow.entity.vo.ResponseVO;
import com.example.orderflow.exception.BusinessException;
import com.example.orderflow.service.ApproveHistoryService;
import com.example.orderflow.service.OrderIdGenerationService;
import com.example.orderflow.service.OrderInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

import static com.example.orderflow.entity.constants.LevelConstants.LEVEL_TWO;
import static com.example.orderflow.entity.constants.LevelConstants.LEVEL_TWO_NAME;

/**
 * @ClassName OrderGeneratedController
 * @Description: 订单操作
 * @Since JDK 1.8
 * @Create 2024/7/12 16:07
 */

@RestController
public class OrderOperateController extends ABaseController {

    @Autowired
    OrderInfoService orderInfoService;

    @Autowired
    OrderIdGenerationService orderIdGenerationService;

    @Autowired
    ApproveHistoryService approveHistoryService;


    OrderInfoConstants orderInfoConstants = new OrderInfoConstants();

    /**
     * @create: 2024/7/12 16:07
     * @description: 订单基本信息初始化
     * @param map
     * @param: orderId 订单号
     * @param: orderType 订单类型
     * @param: orderUnit 订单单位
     * @param: contractNumber 合同号
     * @param: salesManager 销售经理
     * @param: userName 创建人
     * @param: orderDeadline 订单截止日期
     * @return orderId
     */
    @PostMapping("/OrderInitialization")
    public ResponseVO orderInitialization(@RequestBody Map<String, String> map) {

        try {

            //根据订单信息初始化订单并添加进数据库
            orderInfoService.initializationOrderInfo(map.get("orderId"),
                    map.get("orderType"),
                    map.get("orderUnit"),
                    map.get("contractNumber"),
                    map.get("salesManager"),
                    map.get("userName"),
                    map.get("orderDeadline")
            );

            //拆解存入订单号
            orderIdGenerationService.addOrderIdGeneration(map.get("orderId"));

            return getSuccessResponseVO("订单初始化成功");
        }catch (Exception e){
            throw new BusinessException("订单初始化失败，请检查订单号是否存在，如订单号不存在请联系管理员");
        }

    }

    /**
     * @create: 2024/7/12 16:07
     * @description: 订单下达
     * @param map
     * @param: orderId 订单号
     * @param: wmApproveBy 库管审批人
     * @param: peApproveBy 生产工程师审批人
     * @param: pApproveBy 采购审批人
     * @return orderId
     */
    @PostMapping("/OrderFinish")
    public ResponseVO orderFinish(@RequestBody Map<String,String> map) {

        String orderId = map.get("orderId");

        //更新订单审核阶段
        orderInfoService.updateOrderInfoLevelName(orderId,LEVEL_TWO_NAME);
        //更新订单设备总数
        orderInfoService.updateDevicesNumberByOrderId(orderId,map.get("devicesNumber"));

        //创建该订单订单审核记录
        approveHistoryService.createDefaultApproveHistory(orderId,LEVEL_TWO,map.get("wmApproveBy"));
        approveHistoryService.createDefaultApproveHistory(orderId,LEVEL_TWO,map.get("peApproveBy"));
        approveHistoryService.createDefaultApproveHistory(orderId,LEVEL_TWO,map.get("pApproveBy"));

        return getSuccessResponseVO(orderId+"订单下达成功");

    }

    /****
     * @create: 2024/7/12 16:07
     * @description: 订单删除：订单状态ORDER_STATU改为已删除ORDER_STATU_DELETE
     * @param map
     * @param: orderId 订单号
     * @Param: userName 创建人
     * @return orderId
    */
    @PostMapping("/OrderDelete")
    public ResponseVO orderDelete(@RequestBody Map<String, String> map) {
        try {
            //将订单状态改为已删除
            orderInfoService.updateOrderStatuAndVoidedByByOrderIdAndOrderStatuAndVoidedBy(map.get("orderId"),orderInfoConstants.ORDER_STATU_DELETE,map.get("userName"));

            return getSuccessResponseVO("订单删除成功");
        }catch (Exception e){
            throw new BusinessException("订单删除失败，请检查订单号是否存在，其他情况请联系管理员");
        }
    }

    /****
     * @create: 2024/7/12 16:07
     * @description: 更改订单截止日期
     * @param map
     * @param: orderId 订单号
     * @param: orderDeadline 订单截止日期
     * @return orderId
     */
    @PostMapping("/OrderDeadlineChange")
    public ResponseVO orderDeadlineChange(@RequestBody Map<String, String> map) {
        try {
            //更改订单截止日期
            orderInfoService.updateOrderDeadlineByOrderId(map.get("orderId"),map.get("orderDeadline"));
            return getSuccessResponseVO("订单截止日期更改成功");
        }catch (Exception e){
            throw new BusinessException("订单截止日期更改失败，请检查订单号是否存在，其他情况请联系管理员");
        }
    }

}
